package com.tzz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.tzz.config.PreUrlConfig;
import com.tzz.config.SynchBigCustomConfig;
import com.tzz.dao.ProductDao;
import com.tzz.dao.StationDao;
import com.tzz.dao.UserOrderDao;
import com.tzz.entity.UserOrder;
import com.tzz.enums.CommonEnum;
import com.tzz.exception.PreException;
import com.tzz.service.UserOrderService;
import com.tzz.utils.ProducerMQUtil;
import com.tzz.utils.RandomUtil;
import com.tzz.vo.KeyAccountOrderVo;
import com.tzz.vo.UserOrderVo;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.tzz.utils.ModelDataUtil;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Service
public class UserOrderServiceImpl implements UserOrderService {
    @Autowired
    ProductDao productDao;
    @Autowired
    StationDao stationDao;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    PreUrlConfig preUrlConfig;

    @Autowired
    UserOrderDao userOrderDao;

    @Autowired
    SynchBigCustomConfig synchBigCustomConfig;


    @Override
    public List<UserOrderVo> findAllUserOrderKeyAccountOrder(){
        return userOrderDao.selectAll();
    }

    @Override
    public void syncKeyAccountOrder(KeyAccountOrderVo keyAccountOrderVo) {
        JSONObject dataModel = getKeyAccountOrderRequestBody(keyAccountOrderVo);
        DefaultMQProducer producer = ProducerMQUtil.getProducer(synchBigCustomConfig.getGroup());
        ProducerMQUtil.sendMsg(producer,synchBigCustomConfig.getNameServer(), synchBigCustomConfig.getTopic(), dataModel);
        ProducerMQUtil.producerShutDown(producer);
    }

    @Override
    public void createUserOrder(UserOrder userOrder) throws PreException {
        JSONObject createUserOrderModel = getCreateUserOrderModel(userOrder);
        createOrder(createUserOrderModel);
        Date createTime = new Date(System.currentTimeMillis());
        userOrder.setCreateTime(createTime);
        userOrder.setUserOrderId(createUserOrderModel.getString("orderId"));
        if(userOrder.getIsBigCustom() == CommonEnum.IS_BIG_CUSTOM.NO.getCode()){
            userOrder.setKeyAccountOrderId("--");
        }
        userOrderDao.insertUserOrderAndKeyAccountOrder(userOrder);

    }

    private JSONObject getKeyAccountOrderRequestBody(KeyAccountOrderVo keyAccountOrderVo){
        JSONObject dataModel = ModelDataUtil.getRequestDataModel("key_account_order");
        dataModel.put("keyAccountOrderId",keyAccountOrderVo.getKeyAccountOrderId());
        dataModel.put("deliveryStartTime",keyAccountOrderVo.getDeliveryStartTime());
        dataModel.put("deliveryEndTime",keyAccountOrderVo.getDeliveryEndTime());
        ArrayList<HashMap> hashMaps = new ArrayList<>();
        dataModel.put("stationUserOrders",hashMaps);
        for (int i = 0; i < keyAccountOrderVo.getStationUserOrders().size(); i++) {
            HashMap<String, String> stationOrderMap = new HashMap<>();
            stationOrderMap.put("storeMongoId",keyAccountOrderVo.getStationUserOrders().get(i).getStoreMongoId());
            stationOrderMap.put("userOrderId",keyAccountOrderVo.getStationUserOrders().get(i).getUserOrderId());
            hashMaps.add(stationOrderMap);
        }
        return dataModel;
    }

    /**
     * 调用ofc接口
     * */
    private ResponseEntity createOrder(JSONObject createOrderModel){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(createOrderModel, httpHeaders);
        return restTemplate.postForEntity(preUrlConfig.getUrlMap().get("ofcCreateOrderUrl"), httpEntity, JSONObject.class);
    }

    private JSONObject getCreateUserOrderModel(UserOrder userOrder){
        JSONObject createOrderModel = ModelDataUtil.getRequestDataModel("create_order");
        String stationMongo = stationDao.getSingleStationMongoById(userOrder.getStationId());
        createOrderModel.put("outerStationId",stationMongo);
        createOrderModel.put("saleStationMongoId",stationMongo);
        String orderId = RandomUtil.getOrderId();
        createOrderModel.put("orderId",orderId);
        String start_time = String.valueOf(userOrder.getDeliveryStartDate().getTime()/1000);
        String end_time = String.valueOf(userOrder.getDeliveryEndDate().getTime()/1000);
        createOrderModel.put("reservedTimeStart",start_time);
        createOrderModel.put("reservedTimeEnd",end_time);
        createOrderModel.put("userReservedTimeStart",start_time);
        createOrderModel.put("userReservedTimeEnd",end_time);

//        if(userOrder.getIsBigCustom() == 1){
//            createOrderModel.put("bizType",4);
//            createOrderModel.put("bizSubType",22);
//        }
        if(userOrder.getIsBigCustom() == CommonEnum.IS_BIG_CUSTOM.YES.getCode()){
            createOrderModel.put("keyAccountOrderId",userOrder.getKeyAccountOrderId());
            createOrderModel.put("bizType",4);
            createOrderModel.put("bizSubType",22);
        }

        ArrayList<Object> goodList = new ArrayList<>();
        createOrderModel.put("goods",goodList);
        for (int i = 0; i < userOrder.getProductSkuIds().size(); i++) {
            Map<String, Object> goodModel = getGoodModel();
            String productMongo = productDao.getSingleProductMongoBySkuId(userOrder.getProductSkuIds().get(i));
            goodModel.put("id",productMongo);
            goodModel.put("skuId",productMongo);
            goodModel.put("relationId",productMongo);
            goodList.add(goodModel);
        }
        return createOrderModel;
    }


    private Map<String,Object> getGoodModel(){
        ArrayList<Object> ofSizes = new ArrayList<>();
        ArrayList<Object> accessoryDtoList = new ArrayList<>();
        HashMap<String, Object> good = new HashMap<>();
        good.put("id","61b2bb517b37048ddca85d32");
        good.put("skuId","61b2bb517b37048ddca85d32");
        good.put("type",1);
        good.put("productName","TZZzcy1 180gx1");
        good.put("barCode","");
        good.put("fullName","TZZzcy1 180g");
        good.put("count",5);
        good.put("todayLot",0);
        good.put("relationId","61b2bb517b37048ddca85d32");
        good.put("skuLot","");
        good.put("imageUrl","https://imgnew.ddimg.mobi/product/fc04af6e30824db4be859061e7a704fc.jpg?width=800&amp;height=800");
        good.put("weight",180);
        good.put("price",36);
        good.put("totalPrice",36);
        good.put("originPrice",36);
        good.put("fishery",0);
        good.put("ofcSizes",ofSizes);
        good.put("batchType",-1);
        good.put("batchTodayLet",0);
        good.put("detailSeqNo",0);
        good.put("suggestLotStart","");
        good.put("suggestLotEnd","");
        good.put("parentDetailSeqNoType",0);
        good.put("parentDetailSeqNo",null);
        good.put("operateType",null);
        good.put("presaleId","activity202220221215007");
        good.put("accessoryDtoList",accessoryDtoList);
        return good;
    }
}
